/*
 * Copyright 2002-2006 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.jwtools.tmt.ui;

import net.jwtools.tmt.domain.IVisit;
import net.jwtools.tmt.domain.MainVisit;
import net.jwtools.tmt.domain.ReVisit;
import net.jwtools.tmt.domain.Status;
import net.jwtools.tmt.service.IVisitService;

import org.springframework.richclient.application.event.LifecycleApplicationEvent;
import org.springframework.richclient.dialog.CloseAction;
import org.springframework.richclient.dialog.ConfirmationDialog;
import org.springframework.richclient.dialog.FormBackedDialogPage;
import org.springframework.richclient.dialog.TitledPageApplicationDialog;
import org.springframework.richclient.form.Form;
import org.springframework.util.Assert;

/**
 * This is a dialog for editing the properties of a Contact object. It is a
 * simple "form backed" dialog, meaning that the body of the dialog is provided
 * from a "form backed" dialog page. The Ok (finish) button will be wired into
 * the "page complete" state of the dialog page, which in turn gets its state
 * from the automatic validation of the properties on the form.
 * 
 * @author Larry Streepy
 * @see FormBackedDialogPage
 * @see ContactForm
 */
public class TerritoryCheckoutDialog extends TitledPageApplicationDialog {

	/** The form that allows for editing the visit. */
	private Form form;
	private IVisit orgVisit;	

	/** The data store holding all our contacts, used to add a new contact. */
	private IVisitService visitService;

	public TerritoryCheckoutDialog(IVisit visit, IVisitService visitService) {
		Assert.notNull(visitService,
				"The visitService is required to do a checkout");
		IVisit newVisit;
		this.orgVisit=visit;
		if (visit.getNotHomeNumber() != null && visit.getNotHomeNumber() > 0) {
			newVisit = new ReVisit();
			newVisit.setNotHomeNumber(visit.getNotHomeNumber());
			if (visit instanceof MainVisit) {
				newVisit.setBelongsTo(visit);
			} else {
				newVisit.setBelongsTo(visit.getBelongsTo());
			}
		}
		else {
			newVisit = new MainVisit();
		}
		newVisit.setTerritory(visit.getTerritory());
		setCloseAction(CloseAction.DISPOSE);
		form = new TerritoryCheckoutForm(newVisit);
		setDialogPage(new FormBackedDialogPage(form));
		this.visitService = visitService;
	}

	private IVisit getEditingVisit() {
		return (IVisit) form.getFormModel().getFormObject();
	}

	protected void onAboutToShow() {
		setTitle(getMessage("territoryCheckoutDialog.new.title"));
	}

	protected boolean onFinish() {
		// commit any buffered edits to the model
		form.getFormModel().commit();
		// Update the persistent store with the new/modified object.
		
		visitService.save(getEditingVisit());
		orgVisit.setStatus(Status.INACTIVE);
		visitService.save(orgVisit);
		// And notify the rest of the application of the change
		getApplicationContext().publishEvent(
				new LifecycleApplicationEvent(LifecycleApplicationEvent.CREATED, getEditingVisit()));
		getApplicationContext().publishEvent(
				new LifecycleApplicationEvent(LifecycleApplicationEvent.DELETED, orgVisit));
		return true;
	}

	protected void onCancel() {
		// Warn the user if they are about to discard their changes
		if (form.getFormModel().isDirty()) {
			String msg = getMessage("territoryCheckoutDialog.dirtyCancelMessage");
			String title = getMessage("territoryCheckoutDialog.dirtyCancelTitle");
			ConfirmationDialog dlg = new ConfirmationDialog(title, msg) {
				protected void onConfirm() {
					TerritoryCheckoutDialog.super.onCancel();
				}
			};
			dlg.showDialog();
		} else {
			super.onCancel();
		}
	}
}
